//
//  UUSupplierPeakSeasonVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUSupplierPeakSeasonVC.h"
#import "UUSupplierManager.h"
#import "UUResourceManager.h"
#import "UUSupplierPeakSeasonEditVC.h"
#import "UUBaseTableViewCell.h"
#import "UUPermissionManager.h"

@interface UUSupplierPeakSeasonVC () <UITableViewDelegate, UITableViewDataSource, UUSupplierManagerObserver>

@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, copy) NSArray<NSNumber *> *peakSeasons;

@end

@implementation UUSupplierPeakSeasonVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = COMMON_BACKGROUND_COLOR;
    self.tableView.rowHeight = 55;
    [self.tableView registerClass:[UUBaseTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:GetResourceManager().addIconText
                                                                              target:self
                                                                            selector:@selector(addDate:)];
    
    [self reload];
    
    [GetSupplierManager() addObserver:self];
    [GetSupplierManager() asyncUpdatePeakSeasons];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reload {
    self.peakSeasons = [GetSupplierManager() fetchPeakSeasons];
    [self.tableView reloadData];
}

- (IBAction)addDate:(id)sender {
    UUSupplierPeakSeasonEditVC *calendarVC = [UUSupplierPeakSeasonEditVC instanceOfAppropriate];
    [self.navigationController pushViewController:calendarVC animated:YES];
}

- (void)configCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    
}

- (NSNumber *)peakSeasonIdForIndexPath:(NSIndexPath *)indexPath {
    return self.peakSeasons[indexPath.row];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.peakSeasons.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
    
    NSNumber *peakSeasonId = self.peakSeasons[indexPath.row];
    
    UUSupplierPeakSeasonModel *peakSeasonModel = [GetSupplierManager() fetchPeakSeasonModelWithId:peakSeasonId];
    cell.textLabel.text = peakSeasonModel.name;
    
    [self configCell:cell forIndexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSNumber *peakSeasonId = self.peakSeasons[indexPath.row];
    
    if (self.didSelectPeakSeasonBlock == nil) {
        UUSupplierPeakSeasonEditVC *calendarVC = [UUSupplierPeakSeasonEditVC instanceOfAppropriate];
        calendarVC.peakSeasonId = peakSeasonId;
        [self.navigationController pushViewController:calendarVC animated:YES];
    } else {
        self.didSelectPeakSeasonBlock(peakSeasonId);
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return [GetPermissionManager() checkPermission:UUPermissionFlagProductReadWrite];
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *renameAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal
                                                                            title:@"重命名"
                                                                          handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                              NSNumber *peakSeasonId = self.peakSeasons[indexPath.row];
                                                                              UUSupplierPeakSeasonModel *peakSeasonModel = [GetSupplierManager() fetchPeakSeasonModelWithId:peakSeasonId];
                                                                              
                                                                              [UIAlertController presentAlertControllerWithTitle:@"请输入旺季名称"
                                                                                                                         message:nil
                                                                                                            textFieldPlaceholder:nil
                                                                                                                   textFieldText:peakSeasonModel.name
                                                                                                                   selectedRange:NSMakeRange(0, peakSeasonModel.name.length)
                                                                                                                      isPassword:NO
                                                                                                                       doneBlock:^(NSString *newName) {
                                                                                                                           ShowIndicator();
                                                                                                                           WEAK_SELF();
                                                                                                                           [GetSupplierManager() asyncUpdatePeakSeasonWithPeakSeasonId:peakSeasonId
                                                                                                                                                                                  name:newName
                                                                                                                                                                                 dates:peakSeasonModel.dates
                                                                                                                                                                         completeBlock:^(NSError * _Nullable error) {
                                                                                                                                                                             HideIndicatorInView(weakSelf.view);
                                                                                                                                                                             
                                                                                                                                                                             if (error != nil) {
                                                                                                                                                                                 ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                                                                                                                                             }
                                                                                                                                                                         }];
                                                                                                                       } cancelBlock:nil];
                                                                          }];
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
                                                                            title:@"删除"
                                                                          handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                              [UIAlertController presentAlertControllerWithTitle:@"确定删除该旺季吗？" message:nil doneBlock:^{
                                                                                  NSNumber *peakSeasonId = self.peakSeasons[indexPath.row];
                                                                                  
                                                                                  ShowIndicator();
                                                                                  WEAK_SELF();
                                                                                  [GetSupplierManager() asyncDeletePeakSeasonWithId:peakSeasonId completeBlock:^(NSError * _Nullable error) {
                                                                                      HideIndicatorInView(weakSelf.view);
                                                                                      
                                                                                      if (error != nil) {
                                                                                          ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                                                      }
                                                                                  }];
                                                                              } cancelBlock:nil];
                                                                          }];
    return @[deleteAction, renameAction];
}

#pragma mark - UUSupplierManagerObserver methods

- (void)supplierManagerDidUpdatePeakSeasons {
    [self reload];
}

@end
